package com.blacktry.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blacktry.entity.model.ExceptionLog;
import com.blacktry.entity.model.request.ExceptionLogRequest;
import com.blacktry.mapper.ExceptionLogMapper;
import com.blacktry.service.ExceptionLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 *  <p>
 *    服务实现类
 *   </p>
 *
 *   @author blackTry
 *   @since 2022-01-2
 */
@Service
public class ExceptionLogServiceImpl extends ServiceImpl<ExceptionLogMapper, ExceptionLog> implements ExceptionLogService {

    @Override
    public String selectException(String exceptionInfo) {
        StringBuffer sb = new StringBuffer();
        //获取异常信息文本
        String message = exceptionInfo;
        String[] a=message.split("at");
        for(int i=0;i<a.length;i++){
            if(a[i].indexOf("Exception")!=-1||a[i].indexOf("exception")!=-1||a[i].indexOf("blacktry")!=-1||a[i].indexOf("Caused")!=-1){
                sb.append(a[i]);
            }
        }
        return sb.toString();
    }

    @Override
    public Page<ExceptionLog> selectExceptionLog(ExceptionLogRequest exceptionLogRequest, Page<ExceptionLog> logPage) {
        LambdaQueryWrapper<ExceptionLog> wrapper = new LambdaQueryWrapper<>();
        if(exceptionLogRequest!=null&& exceptionLogRequest.getUsername()!=null){
            wrapper.like(ExceptionLog::getOperUserName,exceptionLogRequest.getUsername());
        }
        if(exceptionLogRequest!=null&&exceptionLogRequest.getBeginTime()!=null){
            wrapper.ge(ExceptionLog::getOperCreateTime,exceptionLogRequest.getBeginTime());
        }
        if(exceptionLogRequest!=null&&exceptionLogRequest.getEndTime()!=null){
            wrapper.le(ExceptionLog::getOperCreateTime,exceptionLogRequest.getEndTime());
        }
        wrapper.orderByAsc(ExceptionLog::getOperCreateTime);
        return this.baseMapper.selectPage(logPage,wrapper);
    }



}
